package com.x.erp.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.model.CarFavorite;
import com.x.erp.rpc.CarFavoriteRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;

/**
 * 车龄收藏Controller
 * 
 * @author pansong
 */
@RestController
@RequestMapping(value = "/carFavorite")
public class CarFavoriteController extends BaseController {

	@Reference
	private CarFavoriteRpcService carFavoriteRpcService;// 车辆收藏服务

	/**
	 * 查询车辆收藏列表
	 * 
	 * @param carFavorite
	 * @return
	 */
	@RequestMapping(value = "/getCarFavoriteList", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel getCarFavoriteList(CarFavorite carFavorite) {
		if (carFavorite == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车辆收藏信息不能为空！");
		}
		if (StringUtils.isBlank(carFavorite.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "用户ID不能为空！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功",
				carFavoriteRpcService.selectCarFavoriteList(carFavorite));
	}
	/**
	 * 查询单个车辆收藏信息
	 * 
	 * @param carFavorite
	 * @return
	 */
	@RequestMapping(value = "/getCarFavorite", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel getCarFavorite(CarFavorite carFavorite) {
		if (carFavorite == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车辆收藏信息不能为空！");
		}
		if (StringUtils.isBlank(carFavorite.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "用户ID不能为空！");
		}
		if (StringUtils.isBlank(carFavorite.getCarId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车辆ID不能为空！");
		}
		CarFavorite selectCarFavorite = carFavoriteRpcService.selectCarFavorite(carFavorite);
		Map<String, Object> map = new HashMap<>();
		if(selectCarFavorite == null){
			map.put("favoriteState", 0);
		}else{
			map.put("favoriteState", 1);
			map.put("carFavoriteId", selectCarFavorite.getCarFavoriteId());
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", map);
	}

	/**
	 * 新增车辆收藏
	 * 
	 * @param carCollection
	 * @return
	 */
	@RequestMapping(value = "/insertCarFavorite", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel insertCarCarFavorite(CarFavorite carFavorite) {
		if (carFavorite == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车辆收藏信息不能为空！");
		}
		if (StringUtils.isBlank(carFavorite.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "用户ID不能为空！");
		}
		if (StringUtils.isBlank(carFavorite.getCarId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车辆ID不能为空！");
		}
		if(carFavoriteRpcService.selectCarFavoriteList(carFavorite).size() > 0){
			return new ResultModel(ResultCode.RESULT_FAIL, "您已收藏该车辆！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功",
				carFavoriteRpcService.insertCarFavorite(carFavorite));
	}

	/**
	 * 删除车辆收藏
	 * 
	 * @param carFavorite
	 * @return
	 */
	@RequestMapping(value = "/deleteCarFavorite", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel deleteCarFavorite(CarFavorite carFavorite) {
		if (carFavorite == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "收藏信息不能为空！");
		}
		if (StringUtils.isBlank(carFavorite.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "用户ID不能为空！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "删除成功",
				carFavoriteRpcService.deleteCarFavorite(carFavorite));
	}
}
